﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PivotViewerBase
{
    public class IsolatedStorage
    {

        /// <summary>
        /// Write stream of file to Isolated Storage
        /// </summary>
        public static void WriteToStorage(string FileName, byte[] byteString)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            using (Stream reader = new MemoryStream(byteString))
            {
                using (Stream writer = new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, isoStore))
                {
                    if (writer.CanWrite)
                    {
                        writer.Write(byteString, 0, byteString.Length);
                        writer.Close();
                    }
                }

            }

        }

        /// <summary>
        /// Open Save dialog and write stream from Isolated Storage to Save dialog file
        /// </summary>
        public static byte[] ReadFromStorage(string FileName)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            byte[] byteString;
            using (Stream reader = new IsolatedStorageFileStream(FileName, FileMode.Open, isoStore))
            {
                byteString = ReadFileBytes(reader);
            }
            return byteString;

        }


        public static void DeleteFromStorage(string sFileName)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            isoStore.DeleteFile(sFileName);
        }


        internal static string[] FilesList(string psFilter, string psExt)
        {
              IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

              string[] saFiles = isoStore.GetFileNames(psFilter);
              for (int i = 0; i < saFiles.Length; i++) saFiles[i] = saFiles[i].Replace(psExt, "");

              return saFiles;
        }

        /// <summary>
        /// Write from stream to stream
        /// </summary>
        /// <param name="readStream"></param>
        /// <param name="writeStream"></param>
        private void WriteFileBytes(Stream readStream, Stream writeStream)
        {
            if (writeStream.CanWrite)
            {
                byte[] fileBytes = ReadFileBytes(readStream);
                writeStream.Write(fileBytes, 0, fileBytes.Length);
                writeStream.Close();
            }
        }

        /// <summary>
        /// Create byte[] from stream
        /// </summary>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        private static byte[] ReadFileBytes(Stream fileStream)
        {
            byte[] bytes;
            // Read the source file into a byte array.
            bytes = new byte[fileStream.Length];
            int numBytesToRead = (int)fileStream.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = fileStream.Read(bytes, numBytesRead, numBytesToRead);

                // Break when the end of the file is reached.
                if (n == 0)
                    break;

                numBytesRead += n;
                numBytesToRead -= n;
            }
            numBytesToRead = bytes.Length;
            return bytes;
        }

    }
}
